/**
 * @author 鍚存倍
 * @since 2022/10/13
 * 12:40
 */
package com.windgo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windgo.bean.MarketUser;
import com.windgo.bean.bo.WxCollectBo;
import com.windgo.bean.MarketCollect;
import com.windgo.bean.MarketCollectExample;
import com.windgo.bean.MarketGoods;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.CommonData;
import com.windgo.mapper.MarketCollectMapper;
import com.windgo.mapper.MarketGoodsMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService{
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Override
    public CommonData<MarketCollect> list(BasePageInfo basePageInfo, Integer id) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        marketCollectExample.setOrderByClause(basePageInfo.getSort()+" "+basePageInfo.getOrder());
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        if(id!=null){
            criteria.andIdEqualTo(id);
        }
        List<MarketCollect> list = marketCollectMapper.selectByExample(marketCollectExample);
        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>(list);
        return CommonData.data(marketCollectPageInfo);
    }

    @Override
    public CommonData<MarketCollect> search(BasePageInfo basePageInfo, Integer userId, Integer valueId) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        marketCollectExample.setOrderByClause(basePageInfo.getSort()+" "+basePageInfo.getOrder());
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(valueId!=null){
            criteria.andValueIdEqualTo(valueId);
        }
        List<MarketCollect> list = marketCollectMapper.selectByExample(marketCollectExample);
        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>(list);
        return CommonData.data(marketCollectPageInfo);
    }

    @Override
    public CommonData<WxCollectBo> WxCollect(Byte type, Integer page, Integer limit) {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser) principals.getPrimaryPrincipal();
        Integer id1 = primaryPrincipal.getId();
        PageHelper.startPage(page,limit);
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        if(type!=null){
            criteria.andTypeEqualTo(type);
        }
        if(id1!=null){
            criteria.andUserIdEqualTo(id1);
        }
        ArrayList<WxCollectBo> list = new ArrayList<>();
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        for (MarketCollect marketCollect : marketCollects) {
            Integer id = marketCollect.getId();
            Byte type1 = marketCollect.getType();
            Integer valueId = marketCollect.getValueId();
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(valueId);
            String brief = marketGoods.getBrief();
            String name = marketGoods.getName();
            String picUrl = marketGoods.getPicUrl();
            BigDecimal retailPrice = marketGoods.getRetailPrice();
            WxCollectBo wxCollectBo = new WxCollectBo(brief, id, name, picUrl, retailPrice, type1, valueId);
            list.add(wxCollectBo);
        }
        PageInfo<WxCollectBo> wxCollectBoPageInfo = new PageInfo<>(list);
        return CommonData.data(wxCollectBoPageInfo);

    }
    //加获得userId的接口
    @Override
    public Integer WxCollectDelete(Integer valueId) {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal = (MarketUser)principals.getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();
        MarketCollectExample marketCollectExample1 = new MarketCollectExample();
        MarketCollectExample.Criteria criteria1 = marketCollectExample1.createCriteria();
        criteria1.andUserIdEqualTo(id);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample1);
        if(marketCollects.size()==0){
            Date date = new Date();
           // MarketCollectExample marketCollectExample = new MarketCollectExample();
            //MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
            //criteria.andValueIdEqualTo(marketCollect.getValueId());
            MarketCollect marketCollect1 = new MarketCollect();
            marketCollect1.setDeleted(false);
            marketCollect1.setUpdateTime(date);
            marketCollect1.setAddTime(date);
            marketCollect1.setValueId(valueId);
            marketCollect1.setUserId(id);
            marketCollect1.setType((byte)0);
            int i = marketCollectMapper.insertSelective(marketCollect1);
            //int i = marketCollectMapper.updateByExampleSelective(marketCollect1, marketCollectExample);
            return i;
        }
        for (MarketCollect marketCollect : marketCollects) {
            if(marketCollect.getValueId()==valueId){
                MarketCollectExample marketCollectExample = new MarketCollectExample();
                MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
                criteria.andUserIdEqualTo(id);
                criteria.andValueIdEqualTo(valueId);
                List<MarketCollect> marketCollects1 = marketCollectMapper.selectByExample(marketCollectExample);
               if(marketCollects1.size()==0){
                   Date date = new Date();
                   // MarketCollectExample marketCollectExample = new MarketCollectExample();
                   //MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
                   //criteria.andValueIdEqualTo(marketCollect.getValueId());
                   MarketCollect marketCollect2 = new MarketCollect();
                   marketCollect2.setDeleted(false);
                   marketCollect2.setUpdateTime(date);
                   marketCollect2.setAddTime(date);
                   marketCollect2.setValueId(valueId);
                   marketCollect2.setUserId(id);
                   marketCollect2.setType((byte)0);
                   int i = marketCollectMapper.insertSelective(marketCollect2);
               }
                for (MarketCollect collect : marketCollects1) {
                    if(collect.getDeleted().equals(true)){
                        Date date = new Date();
                        collect.setUpdateTime(date);
                        collect.setDeleted(false);
                        marketCollectMapper.updateByPrimaryKeySelective(collect);
                        return collect.getId();
                    }
                    Date date = new Date();
                    collect.setUpdateTime(date);
                    collect.setDeleted(true);
                    marketCollectMapper.updateByPrimaryKeySelective(collect);
                    return collect.getId();
                }
                /*if(marketCollect.getDeleted().equals(true)){
                    Date date = new Date();
                    MarketCollectExample marketCollectExample = new MarketCollectExample();
                    MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
                    criteria.andValueIdEqualTo(marketCollect.getValueId());
                    MarketCollect marketCollect1 = new MarketCollect();
                    marketCollect1.setDeleted(false);
                    marketCollect1.setUpdateTime(date);
                    int i=marketCollectMapper.updateByPrimaryKeySelective(marketCollect1);
                    return i;
                }else{
                    Date date = new Date();
                    MarketCollectExample marketCollectExample = new MarketCollectExample();
                    MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
                    criteria.andValueIdEqualTo(marketCollect.getValueId());
                    criteria.andUserIdEqualTo(id);
                    MarketCollect marketCollect1 = new MarketCollect();
                    marketCollect1.setDeleted(true);
                    marketCollect1.setUpdateTime(date);
                    //marketCollect1.setValueId(valueId);
                    //marketCollect1.setAddTime(date);
                   // marketCollect1.setDeleted(false);
                  //  marketCollect1.setUserId(id);
                    int i = marketCollectMapper.updateByPrimaryKeySelective(marketCollect1);
                    return i;
                }

                 */
            }

        }
       Date date = new Date();
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        MarketCollect marketCollect1 = new MarketCollect();
        marketCollect1.setDeleted(false);
        marketCollect1.setUpdateTime(date);
        marketCollect1.setAddTime(date);
        marketCollect1.setValueId(valueId);
        marketCollect1.setUserId(id);
        marketCollect1.setType((byte)0);
        int i = marketCollectMapper.insertSelective(marketCollect1);


        //int i = marketCollectMapper.updateByExampleSelective(marketCollect1, marketCollectExample);
        return i;

    }
}

